//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tuliu.appupdate.updatelib;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build.VERSION;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.provider.Settings.System;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.security.MessageDigest;

class Utils {
    private static final String TAG = "Utils";
    private static final String START_ACTIVITY_INDEX = "start_activity_index";
    private static final String START_ACTIVITY_TO_APP_INFO_AUTO_DOWNLOAD_KEY = "auto_download";
    private static final int APP_STORE_INTENT_START_APP_INFO_INDEX = 30;
    static final String APPSTORE_PKGNAME = "com.qihoo.appstore";
    private static final String START_ACTIVITY_SOURCE_STYPE_TYPE = "from_out_side_start_type";
    private static final String START_ACTIVITY_FROM_OUTSIDE_FROM = "from_out_side";
    private static final String LAUNCH_APPSTORE_KEY = "activityKey";
    private static final String LAUNCH_APPSTORE_APP_INFO_ACTIVITY = "appInfo";
    private static final String LAUNCH_APPSTORE_PACKAGE_NAME = "search_content";
    private static final char[] HEX = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    private static String sImei2;
    private static String sImei = "360imei";

    Utils() {
    }

    static boolean isMyNewVersion(Context context, AppInfo info) {
        try {
            PackageInfo e = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return (long)e.versionCode >= info.versionCode;
        } catch (Exception var3) {
            return false;
        }
    }

    static boolean isMyNewPackage(Context context, String packageName, int versionCode) {
        try {
            PackageInfo e = context.getPackageManager().getPackageInfo(packageName, 0);
            return e.versionCode != versionCode;
        } catch (Exception var4) {
            return false;
        }
    }

    static String appendParams(String url, Context context) {
        if(TextUtils.isEmpty(url)) {
            return url;
        } else {
            StringBuilder sb = new StringBuilder(url);
            if(!url.endsWith("?")) {
                sb.append("?");
            }

            String IMEI = getIMEI(context);
            if(!TextUtils.isEmpty(IMEI)) {
                sb.append("m=").append(IMEI);
            }

            String IMEI2 = getIMEI2(context);
            if(!TextUtils.isEmpty(IMEI2)) {
                sb.append("&m2=").append(IMEI2);
            }

            sb.append("&package=").append(context.getPackageName());
            sb.append("&os=").append(VERSION.SDK_INT);
            sb.append("&sdk_version=").append("2.0.8");
            sb.append("&apk_version=").append(getVersionCode(context, context.getPackageName()));
            return sb.toString();
        }
    }


    private static boolean isLowVersion(Context context) {
        boolean result = true;
        PackageManager pm = context.getPackageManager();

        try {
            PackageInfo e = pm.getPackageInfo("com.qihoo.appstore", 0);
            if(e.versionCode >= 199801) {
                result = false;
            }
        } catch (NameNotFoundException var4) {
            var4.printStackTrace();
        }

        return result;
    }

    static int startAppStoreToUpdate(Context context, AppInfo updateInfo, String pkgName, boolean autoDownload) {
        try {
            if(TextUtils.isEmpty(pkgName)) {
                return -1;
            } else if(!isPackageInstalled(context, "com.qihoo.appstore")) {
                return -2;
            } else {
                Intent e = context.getPackageManager().getLaunchIntentForPackage("com.qihoo.appstore");
                e.putExtra("from_out_side_start_type", 1302);
                e.putExtra("startype", 1302);
                e.addFlags(268435456);
                if(isLowVersion(context)) {
                    e.putExtra("activityKey", "appInfo");
                    e.putExtra("search_content", pkgName);
                    e.putExtra("exit_directly", true);
                    context.startActivity(e);
                    return 0;
                } else {
                    e.putExtra("start_activity_index", 30);
                    e.putExtra("from_out_side", pkgName);
                    e.putExtra("pkg_name", pkgName);
                    e.putExtra("auto_download", autoDownload);
                    e.putExtra("exit_directly", true);
                    context.startActivity(e);
                    return 0;
                }
            }
        } catch (Exception var5) {
            if(UpdateManager.isDebug()) {
                Log.e("Utils", "startAppStoreToUpdate fail", var5);
            }

            return -3;
        }
    }

    static boolean isPackageInstalled(Context context, String packageName) {
        try {
            return context.getPackageManager().getPackageInfo(packageName, 0) != null;
        } catch (Exception var3) {
            return false;
        }
    }

    static String getCheckUpdateUrl(Context context, String packageName) {
        String md5 = md5Package(context, packageName);
        int versionCode = getVersionCode(context, packageName);
        StringBuilder sb;
        String imei;
        String imei2;
        if(UpdateManager.isTestMode()) {
            sb = new StringBuilder("http://sdk.test.api.sj.360.cn/free/upgrade?");
            sb.append("package=").append(packageName);
            sb.append("&version=").append(versionCode);
            sb.append("&os=").append(VERSION.SDK_INT);
            sb.append("&md5=").append(md5);
            imei = getIMEI(context);
            if(!TextUtils.isEmpty(imei)) {
                sb.append("&m=").append(imei);
            }

            imei2 = getIMEI2(context);
            if(!TextUtils.isEmpty(imei2)) {
                sb.append("&m2=").append(imei2);
            }

            sb.append("&sdk_version=").append("2.0.8");
            if(UpdateManager.isTestMode()) {
                sb.append("&testmode=" + UpdateManager.getTestMode());
            }

            return sb.toString();
        } else {
            sb = new StringBuilder("http://openbox.mobilem.360.cn/index/upgrade?");
            sb.append("package=").append(packageName);
            sb.append("&version=").append(versionCode);
            sb.append("&os=").append(VERSION.SDK_INT);
            sb.append("&md5=").append(md5);
            imei = getIMEI(context);
            if(!TextUtils.isEmpty(imei)) {
                sb.append("&m=").append(imei);
            }

            imei2 = getIMEI2(context);
            if(!TextUtils.isEmpty(imei2)) {
                sb.append("&m2=").append(imei2);
            }

            sb.append("&sdk_version=").append("2.0.8");
            return sb.toString();
        }
    }

    static void installApk(Context context, String path) {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.setFlags(268435456);
        Uri apkUri = Uri.fromFile(new File(path));
        intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        context.startActivity(intent);
    }

    static String md5Package(Context context, String packageName) {
        try {
            String e = context.getPackageManager().getApplicationInfo(packageName, 0).publicSourceDir;
            return md5(new File(e));
        } catch (NameNotFoundException var3) {
            return null;
        }
    }

    static String md5(File file) {
        FileInputStream fis = null;
        byte[] buffer = new byte[1024];
        boolean numRead = false;

        try {
            fis = new FileInputStream(file);
            MessageDigest md5 = MessageDigest.getInstance("MD5");

            int numRead1;
            while((numRead1 = fis.read(buffer)) > 0) {
                md5.update(buffer, 0, numRead1);
            }

            String e = toHex(md5.digest());
            return e;
        } catch (Exception var15) {
            var15.printStackTrace();
        } finally {
            if(fis != null) {
                try {
                    fis.close();
                } catch (IOException var14) {
                    var14.printStackTrace();
                }
            }

        }

        return null;
    }

    static String md5(String string) {
        try {
            MessageDigest e = MessageDigest.getInstance("MD5");
            e.update(string.getBytes());
            byte[] b = e.digest();
            StringBuffer buf = new StringBuffer("");

            for(int offset = 0; offset < b.length; ++offset) {
                int i = b[offset];
                if(i < 0) {
                    i += 256;
                }

                if(i < 16) {
                    buf.append("0");
                }

                buf.append(Integer.toHexString(i));
            }

            return buf.toString();
        } catch (Exception var6) {
            return null;
        }
    }

    private static String toHex(byte[] b) {
        StringBuilder builder = new StringBuilder();

        for(int i = 0; i < b.length; ++i) {
            byte v = b[i];
            builder.append(HEX[(240 & v) >> 4]);
            builder.append(HEX[15 & v]);
        }

        return builder.toString();
    }

    static String getIMEI(Context c) {
        try {
            if("360imei".equals(sImei)) {
                TelephonyManager e = (TelephonyManager)c.getSystemService(Context.TELEPHONY_SERVICE);
                sImei = e.getDeviceId();
                if(sImei == null) {
                    sImei = "360_DEFAULT_IMEI";
                }
            }
        } catch (Exception var2) {
            ;
        }

        return md5(sImei);
    }

    static String getIMEI2(Context context) {
        try {
            sImei2 = PreferenceManager.getDefaultSharedPreferences(context).getString("app_store_imei", "");
            if(!TextUtils.isEmpty(sImei2)) {
                return sImei2;
            }

            TelephonyManager e = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            String imei = e.getDeviceId();
            String AndroidID = System.getString(context.getContentResolver(), "android_id");
            String serialNo = getDeviceSerial();
            sImei2 = md5(imei + AndroidID + serialNo);
            PreferenceManager.getDefaultSharedPreferences(context).edit().putString("app_store_imei", sImei2).commit();
        } catch (Exception var5) {
            var5.printStackTrace();
        }

        return sImei2;
    }

    private static String getDeviceSerial() {
        String serial = "";

        try {
            Class ignored = Class.forName("android.os.SystemProperties");
            Method get = ignored.getMethod("get", new Class[]{String.class});
            serial = (String)get.invoke(ignored, new Object[]{"ro.serialno"});
        } catch (Exception var3) {
            ;
        }

        return serial;
    }

    static int getVersionCode(Context context, String packageName) {
        try {
            return context.getPackageManager().getPackageInfo(packageName, 0).versionCode;
        } catch (NameNotFoundException var3) {
            var3.printStackTrace();
            return -1;
        }
    }

    private static File getExternalCacheDir(Context context) {
        return VERSION.SDK_INT >= 8?context.getExternalCacheDir():new File(Environment.getExternalStorageDirectory(), "Android/data/" + context.getPackageName() + "/cache");
    }

    private static File getInternalCacheDir(Context context) {
        return context.getCacheDir();
    }

    private static File getCacheDir(Context context) {
        File cacheDir;
        if("mounted".equals(Environment.getExternalStorageState())) {
            cacheDir = getExternalCacheDir(context);
        } else {
            cacheDir = getInternalCacheDir(context);
        }

        if(cacheDir == null) {
            cacheDir = getInternalCacheDir(context);
        }

        return new File(cacheDir, "QihooUpdate");
    }

    static String getUpdateFile(Context context, String url, String packageName) {
        File cacheDir = getCacheDir(context);
        File file = new File(cacheDir, String.format("%s_%s.apk", new Object[]{packageName, md5(url)}));
        if(!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }

        return file.getPath();
    }

    static String getUpdateTempFile(Context context, String url) {
        File cacheDir = getCacheDir(context);
        File file = new File(cacheDir, String.format("%s.qihootmp", new Object[]{md5(url)}));
        if(!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }

        return file.getPath();
    }

    static Bitmap drawableToBitmap(Drawable drawable) {
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();
        Config config = drawable.getOpacity() != -1?Config.ARGB_8888:Config.RGB_565;
        return Bitmap.createBitmap(w, h, config);
    }
}
